package com.yoomiito.app.present.goods;

import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.base.BaseActPresenter;
import com.yoomiito.app.model.bean.GoodsDetail;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.ui.goods.BaoPinGoodsInfoActivity;
import com.yoomiito.app.utils.NetworkUtils;
import com.yoomiito.app.utils.YMToast;

import java.util.List;

import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.BaseModel;
import cn.droidlover.xdroidmvp.net.XApi;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

@Deprecated
public class BaoPinGoodsInfoPresenter extends BaseActPresenter<BaoPinGoodsInfoActivity> {

    public BaoPinGoodsInfoPresenter(AppConfig appConfig) {
        super(appConfig);
    }

    /**
     * 商品详情
     * @param id
     */
    public void getGoodsInfo(String id,String type){
        if (NetworkUtils.checkNetPermission(getV())){
            return;
        }
        YmApi.getYmService()
                .getGoodsInfo(id,type)
                .compose(getV().bindToLifecycle())
                .compose(XApi.<GoodsDetail>getApiTransformerData())
                .compose(XApi.<GoodsDetail>getScheduler())
                .subscribe(new ApiSubscriber<GoodsDetail>(handleException()) {
                    @Override
                    public void onSuccess(GoodsDetail detail) {
                        getV().setGoodsInfo(detail);
                        getV().dismissLoading();
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
//                        if (300==error.getCode()){//商品下架
                            getV().pullMarket(error.getMsg());
//                        }
                    }
                });
    }

    /**
     * 关联商品
     *
     * @param id
     */
    public void getRelatedGoods(String id){
        if (NetworkUtils.checkNetPermission(getV())){
            return;
        }
        YmApi.getYmService()
                .getRelatedGoods(id)
                .compose(getV().bindToLifecycle())
                .compose(XApi.<List<GoodsDetail>>getApiTransformerData())
                .compose(XApi.<List<GoodsDetail>>getScheduler())
                .subscribe(new ApiSubscriber<List<GoodsDetail>>(handleException()) {
                    @Override
                    public void onSuccess(List<GoodsDetail> goodsDetails) {
                        getV().setRecommendGoods(goodsDetails);
                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }

    /**
     * 添加收藏
     * @param id
     */
    public void addCollection(String id){
        if (NetworkUtils.checkNetPermission(getV())){
            return;
        }
        YmApi.getYmService()
                .addCollection(id)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>(handleException()) {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        getV().showToast("收藏成功");
                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }

    /**
     * 取消收藏
     * @param id
     */
    public void delCollection(String id){
        if (NetworkUtils.checkNetPermission(getV())){
            return;
        }
        YmApi.getYmService()
                .delCollection(id)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>(handleException()) {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        getV().showToast("已取消收藏");
                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }

    //0元购商详
    public void getFreeGoodsInfo(String id,String productId){
        YmApi.getYmService()
                .getFreeGoodsInfo(id, productId)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<GoodsDetail>(handleException()) {
                    @Override
                    public void onSuccess(GoodsDetail goodsDetail) {
                        getV().setGoodsInfo(goodsDetail);
                        getV().dismissLoading();
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
//                        if (ApiException.GOODS_OBTAINED==error.getCode()){//商品下架
                            getV().pullMarket("");
//                        }
                    }
                });
    }

    //领取免费商品
    public void buyFreeGoods(String id,String productId){
        YmApi.getYmServiceNoErrorCode().buyFreeGoods(id,productId)
                .compose(getV().bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(freeGoodsBuyInfoBaseModel -> {
                    if (freeGoodsBuyInfoBaseModel.getCode()==200){
                        getV().buyFreeGoods(freeGoodsBuyInfoBaseModel.data.getSend_url());
                    }else if (freeGoodsBuyInfoBaseModel.getCode()==5002){
                        getV().showFreeGoodsCouponDialog(freeGoodsBuyInfoBaseModel.getData());
                    }else {
                        YMToast.showText(freeGoodsBuyInfoBaseModel.msg);
                    }
                });

    }

}
